//
//  ReadViewController.m
//  ReadDemo
//
//  Created by YPShao on 2017/10/17.
//  Copyright © 2017年 YPShao. All rights reserved.
//

#import "ReadViewController.h"
#import "YYTextView.h"
#import "HMTextLabel.h"
#import "YYText.h"
#import "NSAttributedString+YYText.h"
#import "HMReadView.h"
#import "UIView+Convenience.h"

@interface ReadViewController ()

@property (nonatomic, strong) YYTextView *textView;
@property (nonatomic, strong) HMTextLabel *textLabel;

@end

@implementation ReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    [self setUpView];
//    [self setReadView];
    [self setUpTextLabel];
    
    UIButton *chagenbutton = [[UIButton alloc]initWithFrame:CGRectMake(30, self.view.frame.size.height- 50, 40, 40)];
    chagenbutton.backgroundColor = [UIColor redColor];
    [self.view addSubview:chagenbutton];
    [chagenbutton addTarget:self action:@selector(onChangeButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setUpTextLabel
{
    
    HMTextLabel *textLabel = [HMTextLabel new];
    self.textLabel = textLabel;
    textLabel.backgroundColor = [UIColor colorWithWhite:0.920 alpha:1.000];
    textLabel.text = @"HMTextLabel在我还没长开的时候，就常听人说，有两种男人不能嫁：一种是兵哥哥，另一种是程序员。前者见不着，后者死的早。一想到不等头发花白，就要踟蹰独行，我就害怕的厉害。所以，很长一段时间，都对身边的程序员们避而远之。甚至做梦也没有想到，自己会成为程序员的老婆，而且一做就是好多年。嫁给程序员老公，我后悔了，后悔我爱他还不够，后悔我关心他还不足，后悔我欺负他还太多。当然最后悔的还是，我遇见他还太迟。虽然和预先设想的略有差异，但还是感动成了流浪狗。这么多年风风雨雨过来，我们虽然不曾大富大贵，但日子却过e first of these methods actually draws an appropriate underline for the glyph range given.  The second method potentially breaks the range it is given up into subranges and calls drawUnderline... for ranges that should actually have the underline drawn.  As examples of why there are two methods, consider two situations.  First, in all cases you don't want to underline the leading and trailing whitespace on a line. ";
    textLabel.textColor = [UIColor blackColor];
    textLabel.font = [UIFont systemFontOfSize:10.0f];
    textLabel.numberOfLines = 0;
    textLabel.textAlignment = NSTextAlignmentJustified;
    textLabel.lineSpacing = 10.0f;
    textLabel.adjustsFontSizeToFitWidth = NO;
    textLabel.textInsets = UIEdgeInsetsZero;
    textLabel.frame = CGRectMake(10.0f, 50, self.view.frame.size.width, 100.0f);
    [self.view addSubview:textLabel];
    textLabel.frameWidth = self.view.frameWidth - 10.0f*2;
    [textLabel sizeToFit];
    [textLabel setHighLightRange:NSMakeRange(10, 30)];
}

- (void)setReadView
{
    HMReadView *readView = [[HMReadView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 80)];
    readView.textView.text = @"readView在我还没长开的时候，就常听人说，有两种男人不能嫁：一种是兵哥哥，另一种是程序员。前者见不着，后者死的早。一想到不等头发花白，就要踟蹰独行，我就害怕的厉害。所以，很长一段时间，都对身边的程序员们避而远之。甚至做梦也没有想到，自己会成为程序员的老婆，而且一做就是好多年。嫁给程序员老公，我后悔了，后悔我爱他还不够，后悔我关心他还不足，后悔我欺负他还太多。当然最后悔的还是，我遇见他还太迟。虽然和预先设想的略有差异，但还是感动成了流浪狗。这么多年风风雨雨过来，我们虽然不曾大富大贵，但日子却过he first of these methods actually draws an appropriate underline for the glyph range given.  The second method potentially breaks the range it is given up into subranges and calls drawUnderline... for ranges that should actually have the underline drawn.  As examples of why there are two methods, consider two situations.  First, in all cases you don't want to underline the leading and trailing whitespace on a line.  The -underlineGlyphRange... method is passed glyph ranges that have underlining turned on, but it will then look for this leading and trailing white space and only pass the ranges that should actually be underlined to -drawUnderline...  Second, if the underlineType: indicates that only words, (i.e., no whitespace), should be underlined, then -underlineGlyphRange... will carve the range it is passed up into words and only pass word ranges to -drawUnderline.在我还没长开的时候，就常听人说，有两种男人不能嫁：一种是兵哥哥，另一种是程序员。前者见不着，后者死的早。一想到不等头发花白，就要踟蹰独行，我就害怕的厉害。所以，很长一段时间，都对身边的程序员们避而远之。甚至做梦也没有想到，自己会成为程序员的老婆，而且一做就是好多年。嫁给程序员老公，我后悔了，后悔我爱他还不够，后悔我关心他还不足，后悔我欺负他还太多。当然最后悔的还是，我遇见他还太迟。虽然和预先设想的略有差异，但还是感动成了流浪狗。这么多年风风雨雨过来，我们虽然不曾大富大贵，但日子却过的祥和宁静";
    [self.view addSubview:readView];
    
    [readView setHighLightRange:NSMakeRange(10, 200)];
    
}

- (void)setUpView
{
    NSString *textString = @" The first of these methods actually draws an appropriate underline for the glyph range given.  The second method potentially breaks the range it is given up into subranges and calls drawUnderline... for ranges that should actually have the underline drawn.  As examples of why there are two methods, consider two situations.  First, in all cases you don't want to underline the leading and trailing whitespace on a line.  The -underlineGlyphRange... method is passed glyph ranges that have underlining turned on, but it will then look for this leading and trailing white space and only pass the ranges that should actually be underlined to -drawUnderline...  Second, if the underlineType: indicates that only words, (i.e., no whitespace), should be underlined, then -underlineGlyphRange... will carve the range it is passed up into words and only pass word ranges to -drawUnderline.在我还没长开的时候，就常听人说，有两种男人不能嫁：一种是兵哥哥，另一种是程序员。前者见不着，后者死的早。一想到不等头发花白，就要踟蹰独行，我就害怕的厉害。所以，很长一段时间，都对身边的程序员们避而远之。甚至做梦也没有想到，自己会成为程序员的老婆，而且一做就是好多年。嫁给程序员老公，我后悔了，后悔我爱他还不够，后悔我关心他还不足，后悔我欺负他还太多。当然最后悔的还是，我遇见他还太迟。虽然和预先设想的略有差异，但还是感动成了流浪狗。这么多年风风雨雨过来，我们虽然不曾大富大贵，但日子却过的祥和宁静。The first of these methods actually draws an appropriate underline for the glyph range given.  The second method potentially breaks the range it is given up into subranges and calls drawUnderline... for ranges that should actually have the underline drawn.  As examples of why there are two methods, consider two situations.  First, in all cases you don't want to underline the leading and trailing whitespace on a line.  The -underlineGlyphRange... method is passed glyph ranges that have underlining turned on, but it will then look for this leading and trailing white space and only pass the ranges that should actually be underlined to -drawUnderline...  Second, if the underlineType: indicates that only words, (i.e., no whitespace), should be underlined, then -underlineGlyphRange... will carve the range it is passed up into words and only pass word ranges to -drawUnderline.在我还没长开的时候，就常听人说，有两种男人不能嫁：一种是兵哥哥，另一种是程序员。前者见不着，后者死的早。一想到不等头发花白，就要踟蹰独行，我就害怕的厉害。所以，很长一段时间，都对身边的程序员们避而远之。甚至做梦也没有想到，自己会成为程序员的老婆，而且一做就是好多年。嫁给程序员老公，我后悔了，后悔我爱他还不够，后悔我关心他还不足，后悔我欺负他还太多。当然最后悔的还是，我遇见他还太迟。虽然和预先设想的略有差异，但还是感动成了流浪狗。这么多年风风雨雨过来，我们虽然不曾大富大贵，但日子却过的祥和宁静。";
    YYTextView *textView = [[YYTextView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 80)];
    [self.view addSubview:textView];
    textView.font = [UIFont systemFontOfSize:18];
    self.textView = textView;
    textView.text = textString;

    
    YYTextBorder *border = [YYTextBorder borderWithFillColor:[UIColor grayColor] cornerRadius:3];
    
    YYTextHighlight *highlight = [YYTextHighlight new];
    [highlight setColor:[UIColor redColor]];
    [highlight setBackgroundBorder:border];
    highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        NSLog(@"tap text range:...");
        // 你也可以把事件回调放到 YYLabel 和 YYTextView 来处理。
    };
    // 2. 把"高亮"属性设置到某个文本范围
    NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc]initWithString:textString];
    [attributedText yy_setTextHighlight:highlight range:NSMakeRange(10, 100)];
    textView.attributedText = attributedText;
    
//    HMTextLabel *textLabel = [[HMTextLabel alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 80)];
//    textLabel.textString = @"在我还没长开的时候，就常听人说，有两种男人不能嫁：一种是兵哥哥，另一种是程序员。前者见不着，后者死的早。一想到不等头发花白，就要踟蹰独行，我就害怕的厉害。所以，很长一段时间，都对身边的程序员们避而远之。甚至做梦也没有想到，自己会成为程序员的老婆，而且一做就是好多年。嫁给程序员老公，我后悔了，后悔我爱他还不够，后悔我关心他还不足，后悔我欺负他还太多。当然最后悔的还是，我遇见他还太迟。虽然和预先设想的略有差异，但还是感动成了流浪狗。这么多年风风雨雨过来，我们虽然不曾大富大贵，但日子却过的祥和宁静。";
//    [self.view addSubview:textLabel];
    
    
//    [textLabel setHighLightRange:NSMakeRange(10, 100)];
}

- (void)onButtonClick:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)onChangeButtonClick
{
    self.textLabel.font  = [UIFont systemFontOfSize:20];
    self.textLabel.frameWidth = self.view.frameWidth - 10.0f*2;
    [self.textLabel sizeToFit];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
